
package edu.hm.devcamp.communication;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;

import android.app.IntentService;
import android.content.Intent;
import edu.hm.devcamp.communication.json.JsonParser;
import edu.hm.devcamp.providers.Poi;

public class PostPutDeleteService extends IntentService {

    public static final String POI_EXTRA = "poi";

    public static final String METHOD_EXTRA = "method";

    public static final int POST = 1;

    public static final int PUT = 2;

    public static final int DELETE = 3;

    private static final String sUrl = "munichpoi.appspot.com/api/pois/";
    
    private static final String sDeleteUrl = sUrl + "delete/";

    private HttpClient mClient = new DefaultHttpClient();
    
    private String mOutString = "";

    public PostPutDeleteService() {
        super("PostPutDeleteService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Poi poi = (Poi)intent.getSerializableExtra(POI_EXTRA);

        if (poi != null) {
            try {
                mOutString = JsonParser.parseToString(poi);
            } catch (JsonGenerationException e) {
                throw new RuntimeException(e);
            } catch (JsonMappingException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            
            switch (intent.getIntExtra(METHOD_EXTRA, -1)) {
                case POST:
                    post();
                    break;
                case PUT:
                    put();
                    break;
                case DELETE:
                    delete(poi.id);
                    break;
                default:
                    break;
            }
        }
    }

    private boolean post() {
        try{        
            HttpPost post = new HttpPost(sUrl);
            HttpEntity entity = new StringEntity(mOutString);
            post.setEntity(entity);
            
            request(post);
        }catch(UnsupportedEncodingException e){
            throw new RuntimeException(e);
        }
        
        return false;
    }

    private boolean put() {
        try{        
            HttpPut put = new HttpPut(sUrl);
            HttpEntity entity = new StringEntity(mOutString);
            put.setEntity(entity);
            
//            request(put);
        }catch(UnsupportedEncodingException e){
            throw new RuntimeException(e);
        }
        
        return false;
    }

    private boolean delete(long id) {            
        HttpDelete delete = new HttpDelete(sDeleteUrl + id);            
//        request(delete);
                
        return false;
    }

    private Poi request(HttpPost request) {
        HttpResponse response;
        InputStream instream = null;
        Poi poi = null;
        
        try {
            
         
            response = mClient.execute(request);

            String status = response.getStatusLine().toString();
            if (!status.contains("200")) {
                throw new IllegalResponseException("An error occured during the server communication: " + status);
            }
            
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                instream = entity.getContent();
                poi = JsonParser.parse(instream);                
            }
            
            return poi;
        } catch(ClientProtocolException e){
            throw new RuntimeException(e);
        } catch(IOException e){
            throw new RuntimeException(e);
        } finally {
            if (instream != null){
                try {
                    instream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            request.abort();
        }
    }
}